package org.itworks.biodev.gui;

import java.awt.event.*;
import java.text.*;
import java.util.*;

import javax.swing.*;

import org.itworks.biodev.model.Customer;
import org.itworks.biodev.model.Meeting;
import org.itworks.helpers.MVCNotification;

public class EditParticipantToMeetingDialogView extends JDialog implements Observer {
	public static final int FILL_COMBOBOX_CUSTOMERS = 0;

	private EditParticipantToMeetingDialogControler controler;
	
	private javax.swing.JLabel addParticipantTitleLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox curstomersListComboBox;
    private javax.swing.JLabel hostMeetingLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel searchForCustomerLabel;
    private javax.swing.JTextField searchForCustomerTextField;
    
    private boolean isOK = false;
	
	public EditParticipantToMeetingDialogView(EditParticipantToMeetingDialogControler controler) {
		this.controler = controler;
		
		this.controler.getModel().addObserver(this);
    	
    	initComponents();
    	initHandlers();
    	
    	controler.viewLoaded();
	}

	private void initHandlers() {
		// TODO Auto-generated method stub
		
		this.okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				// TODO Auto-generated method stub
				int selectedIndex = curstomersListComboBox.getSelectedIndex();
				controler.okButtonActionPerformed(actionEvent, selectedIndex);
				isOK = true;
				setVisible(false);
			}
			
		});
		
		this.cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				// TODO Auto-generated method stub
				setVisible(false);
				
			}
			
		});
		
	}

	private void initComponents() {
		setTitle("Editer un participant");
		setLocationRelativeTo(null);
		
		
		// TODO Auto-generated method stub
		hostMeetingLabel = new javax.swing.JLabel();
        curstomersListComboBox = new javax.swing.JComboBox();
        searchForCustomerLabel = new javax.swing.JLabel();
        searchForCustomerTextField = new javax.swing.JTextField();
        addParticipantTitleLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        hostMeetingLabel.setText("Participant :");

        searchForCustomerLabel.setText("Recherche rapide par nom :");

        addParticipantTitleLabel.setFont(new java.awt.Font("Trebuchet MS", 1, 18)); // NOI18N
        addParticipantTitleLabel.setText("Ajouter un participant");

        okButton.setText("OK");

        cancelButton.setText("Annuler");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(hostMeetingLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addComponent(curstomersListComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(searchForCustomerLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchForCustomerTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(addParticipantTitleLabel)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addParticipantTitleLabel)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hostMeetingLabel)
                    .addComponent(searchForCustomerTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchForCustomerLabel)
                    .addComponent(curstomersListComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
		
	}

	@Override
	public void update(Observable srcObject, Object object) {
		// TODO Auto-generated method stub
		MVCNotification notification = (MVCNotification)object;
		int reason = notification.getReason();
		
		switch(reason) {
		case FILL_COMBOBOX_CUSTOMERS:
			fillComboBoxCustomers((Vector<Customer>)notification.getObject());
			break;
		
		
		}
	}

	private void fillComboBoxCustomers(Vector<Customer> customers) {
		// TODO Auto-generated method stub
		this.curstomersListComboBox.removeAllItems();
		for(Customer c : customers) {
			this.curstomersListComboBox.addItem(c.toString());
		}
	}
	
	public boolean getIsOk() {
		return this.isOK;
	}
}
